<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-input
            v-model="teamName"
            placeholder="请输入队伍名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container" style="height: 750px;overflow: auto;display: -webkit-box;-webkit-overflow-scrolling: touch;">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="value"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="19" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="账号注册时间">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeRegisterDatetime"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="加入志愿者队伍" prop="teamId">-->
<!--        <el-input-->
<!--          v-model="queryParams.teamId"-->
<!--          placeholder="请输入加入志愿者队伍"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="工作单位，用户自己填" prop="company">-->
<!--        <el-input-->
<!--          v-model="queryParams.company"-->
<!--          placeholder="请输入工作单位，用户自己填"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="政治身份，1 中共党员 2 中共预备党员 3 共青团员 4 群众" prop="politicalIdentityId">-->
<!--        <el-input-->
<!--          v-model="queryParams.politicalIdentityId"-->
<!--          placeholder="请输入政治身份，1 中共党员 2 中共预备党员 3 共青团员 4 群众"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="联系地址" prop="contactAddress">-->
<!--        <el-input-->
<!--          v-model="queryParams.contactAddress"-->
<!--          placeholder="请输入联系地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="积分" prop="point">-->
<!--        <el-input-->
<!--          v-model="queryParams.point"-->
<!--          placeholder="请输入积分"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="工时" prop="workHours">-->
<!--        <el-input-->
<!--          v-model="queryParams.workHours"-->
<!--          placeholder="请输入工时"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="生日">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeBirthDate"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="特长，掌握的(生活)技能" prop="forte">-->
<!--        <el-input-->
<!--          v-model="queryParams.forte"-->
<!--          placeholder="请输入特长，掌握的(生活)技能"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="用户id" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入用户id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['volunteer:volunteer:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['volunteer:volunteer:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['volunteer:volunteer:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['volunteer:volunteer:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="volunteerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="pkId" />-->
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
<!--      <el-table-column label="所属慈善机构，管理员分配" align="center" prop="organizationId" />-->
      <el-table-column label="账号注册时间" align="center" prop="registerDatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作单位" align="center" prop="company" />
      <el-table-column label="政治身份" align="center" prop="politicalIdentityId">
        <template slot-scope="scope">
          <span v-if="scope.row.politicalIdentityId===0">群众</span>
          <span v-if="scope.row.politicalIdentityId===1">中共党员</span>
          <span v-if="scope.row.politicalIdentityId===2">中共预备党员</span>
          <span v-if="scope.row.politicalIdentityId===3">共青团员</span>
          <span v-if="scope.row.politicalIdentityId===4">无党派民主人士</span>
        </template>
      </el-table-column>
      <el-table-column label="加入志愿说明" align="center" prop="joinDescription" />
      <el-table-column label="联系地址" align="center" prop="contactAddress" />
      <el-table-column label="生日" align="center" prop="birthDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span v-if="scope.row.sex===0">男</span>
          <span v-if="scope.row.sex===1">女</span>
          <span v-if="scope.row.sex===2">未知</span>
        </template>
      </el-table-column>
      <el-table-column label="特长" align="center" prop="forte" />
      <el-table-column label="队伍" align="center" prop="teamName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status===0">审核中</span>
          <span v-if="scope.row.status===1">审核通过</span>
          <span v-if="scope.row.status===2">审核拒绝</span>
        </template>
      </el-table-column>
      <el-table-column  width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status!==0"
            size="small"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:volunteer:pass']"
          >同意</el-button>
          <el-button
            :disabled="scope.row.status!==0"
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:volunteer:deny']"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      </el-col>
    </el-row>
    <!-- 添加或修改志愿者审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="120px">
        <el-form-item label="服务网点:" prop="organizeId">
          <el-select v-model="form2.organizeId" filterable placeholder="请选择">
            <el-option
              v-for="item in wllist"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVolunteer, getVolunteer, delVolunteer, addVolunteer, updateVolunteer,organization,passVolunteer,denyVolunteer } from "@/api/volunteer/volunteer";
import {teamlist} from "@/api/user/user";
export default {
  watch: {
    teamName(val) {
      this.$refs.tree.filter(val);
    }
  },
  name: "Volunteer",
  data() {
    return {
      // 队伍名称
      teamName:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 志愿者审核表格数据
      volunteerList: [],
      // 弹出层标题
      title: "",
      duiwoptions:[],
      deptOptions:[
      {
          value: 1,
          label: '志愿者队伍',
          children: []
        }],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 网络列表
      wllist:[],
      form2:{
        pkId:''
      },
      // 是否显示弹出层
      open: false,
      // 用户id时间范围
      daterangeRegisterDatetime: [],
      // 用户id时间范围
      daterangeBirthDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pkId: null,
        phone: null,
        realName: null,
        organizationId: null,
        registerDatetime: null,
        teamId: null,
        company: null,
        politicalIdentityId: null,
        joinDescription: null,
        contactAddress: null,
        point: null,
        workHours: null,
        birthDate: null,
        sex: null,
        forte: null,
        status: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        organizeId: [
          { required: true, message: '必须选择服务网点', trigger: 'change'}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询志愿者审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRegisterDatetime && '' != this.daterangeRegisterDatetime) {
        this.queryParams.params["beginRegisterDatetime"] = this.daterangeRegisterDatetime[0];
        this.queryParams.params["endRegisterDatetime"] = this.daterangeRegisterDatetime[1];
      }
      if (null != this.daterangeBirthDate && '' != this.daterangeBirthDate) {
        this.queryParams.params["beginBirthDate"] = this.daterangeBirthDate[0];
        this.queryParams.params["endBirthDate"] = this.daterangeBirthDate[1];
      }
      listVolunteer(this.queryParams).then(response => {
        this.volunteerList = response.pageData;
        console.log(this.volunteerList)
        this.total =Number(response.total) ;
        console.log(this.volunteerList)
        this.loading = false;
      });
      teamlist().then(res =>{
        this.duiwoptions = res
        this.duiwoptions = this.duiwoptions.map(item =>({
          label:item.name,
          value:item.id
        }))
        this.deptOptions[0].children =  this.duiwoptions
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pkId: null,
        phone: null,
        realName: null,
        organizationId: null,
        registerDatetime: null,
        teamId: null,
        company: null,
        politicalIdentityId: null,
        joinDescription: null,
        contactAddress: null,
        point: null,
        workHours: null,
        birthDate: null,
        sex: null,
        forte: null,
        status: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRegisterDatetime = [];
      this.daterangeBirthDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加志愿者审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // this.form2.approveId = String(row.pkId)
      // this.form2.userId = String(row.userId)
      // const pkId = row.pkId || this.ids
      // organization().then(response => {
      //   console.log(response.data,'哈哈哈哈')
      //   this.wllist = response.data
      //   this.open = true;
      //   this.title = "同意列表";
      // });
      this.form2.pkId = row.approveId
      console.log(row.approveId)
      passVolunteer(row.approveId).then(res => {
        this.$modal.msgSuccess("同意成功");
        this.open = false;
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form2)
      this.$refs["form2"].validate(valid => {
        if (valid) {
          passVolunteer(this.form2).then(res => {
              this.$modal.msgSuccess("同意成功");
              this.open = false;
              this.getList();
            })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.form2.teamApproveId = row.pkId
      this.form2.userId = row.userId
      this.form2.volunteerApproveId = row.approveId
      console.log(this.form2)
      const pkIds = row.pkId || this.ids;
      this.$modal.confirm('是否确认拒绝志愿者').then(() =>{
        console.log('11111111111')
      denyVolunteer(this.form2).then(res=>{
        console.log(res)
      });
      }).then(() => {
        console.log('222222222')
        this.getList();
        this.$modal.msgSuccess("拒绝成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('volunteer/volunteer/export', {
        ...this.queryParams
      }, `volunteer_${new Date().getTime()}.xlsx`)
    },
    // 树形结构
    handleNodeClick(data) {
      this.teamName = data.label
      this.teamId = data.value
      if (data.value === -1){
        this.queryParams.teamId = null
        this.queryParams.role = 0
        this.getList()
      }else {
        console.log(data.value);
        this.queryParams.teamId = data.value
        this.queryParams.role = 1
        this.getList()
      }
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    }
  }
};
</script>
